home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tdeque.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  3KB  |  169 lines

  1. /*
  2.   test of Deques
  3. */
  4.  
  5. #ifdef PTIMES
  6. const int ptimes = 1;
  7. #else
  8. const int ptimes = 0;
  9. #endif
  10.  
  11. #include <stream.h>
  12. #include <assert.h>
  13.  
  14. #include "iDeque.h"
  15.  
  16. #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
  17.                        else _assert(#ex, __FILE__,__LINE__); }
  18.  
  19.  
  20. int SIZE;
  21.  
  22. void print(intDeque& a)
  23. {
  24.   int maxprint = 20;
  25.   cout << "[";
  26.   int k = 0;
  27.   while (!a.empty() && k++ < maxprint)
  28.     cout << a.deq() << " ";
  29.   if (k == maxprint) 
  30.     cout << "]\n";
  31.   else
  32.   {
  33.     while (!a.empty()) a.del_front();
  34.     cout << "...]\n";
  35.   }
  36.   assert(a.empty());
  37. }
  38.  
  39. #include "iXPDeque.h"
  40.  
  41. void XPtest () 
  42. {
  43.   intXPDeque d(SIZE);
  44.   assert(d.OK());
  45.   for (int i = 0; i < SIZE; ++i)
  46.   {
  47.     if (i % 2 == 0)
  48.       d.enq(i);
  49.     else
  50.       d.push(i);
  51.   }
  52.   assert(d.length() == SIZE);
  53.   assert(d.front() == (SIZE-1));
  54.   assert(d.rear() == (SIZE-2));
  55.   assert(!d.full());
  56.   intXPDeque d1(SIZE/2);
  57.   for (i = (SIZE-1); i >= 0; --i)
  58.   {
  59.     int x;
  60.     if (i % 2 == 0)
  61.     {
  62.       x = d.rear();
  63.       d.del_rear();
  64.     }
  65.     else
  66.     {
  67.       x = d.front();
  68.       d.del_front();
  69.     }
  70.     d1.enq(x);
  71.   }
  72.   assert(d.empty());
  73.   assert(d1.length() == SIZE);
  74.   assert(d1.front() == (SIZE-1));
  75.   assert(d1.rear() == 0);
  76.   assert(d.OK());
  77.   assert(d1.OK());
  78.   intXPDeque d2 (d1);
  79.   assert(d2.length() == SIZE);
  80.   assert(d2.front() == (SIZE-1));
  81.   assert(d2.OK());
  82.   d1.clear();
  83.   assert(d1.empty());
  84.   d1 = d2;
  85.   assert(d1.length() == SIZE);
  86.   assert(d1.front() == (SIZE-1));
  87.   cout << "d1:"; print(d1);
  88.   assert(d.OK());
  89.   assert(d1.OK());
  90.   assert(d2.OK());
  91. }
  92.  
  93.  
  94. #include "iDLDeque.h"
  95.  
  96. void DLtest () 
  97. {
  98.   intDLDeque d;
  99.   assert(d.OK());
  100.   for (int i = 0; i < SIZE; ++i)
  101.   {
  102.     if (i % 2 == 0)
  103.       d.enq(i);
  104.     else
  105.       d.push(i);
  106.   }
  107.   assert(d.length() == SIZE);
  108.   assert(d.front() == (SIZE-1));
  109.   assert(d.rear() == (SIZE-2));
  110.   assert(!d.full());
  111.   intDLDeque d1;
  112.   for (i = (SIZE-1); i >= 0; --i)
  113.   {
  114.     int x;
  115.     if (i % 2 == 0)
  116.     {
  117.       x = d.rear();
  118.       d.del_rear();
  119.     }
  120.     else
  121.     {
  122.       x = d.front();
  123.       d.del_front();
  124.     }
  125.     d1.enq(x);
  126.   }
  127.   assert(d.empty());
  128.   assert(d1.length() == SIZE);
  129.   assert(d1.front() == (SIZE-1));
  130.   assert(d1.rear() == 0);
  131.   assert(d.OK());
  132.   assert(d1.OK());
  133.   intDLDeque d2 (d1);
  134.   assert(d2.length() == SIZE);
  135.   assert(d2.front() == (SIZE-1));
  136.   assert(d2.OK());
  137.   d1.clear();
  138.   assert(d1.empty());
  139.   d1 = d2;
  140.   assert(d1.length() == SIZE);
  141.   assert(d1.front() == (SIZE-1));
  142.   cout << "d1:"; print(d1);
  143.  
  144.   assert(d.OK());
  145.   assert(d1.OK());
  146.   assert(d2.OK());
  147. }
  148.  
  149.  
  150. int main(int argv, char** argc)
  151. {
  152.   if (argv > 1)
  153.   {
  154.     SIZE = abs(atoi(argc[1]));
  155.     SIZE &= ~1;
  156.   }
  157.   else
  158.     SIZE = 100;
  159.  
  160.   start_timer();
  161.   cout << "XP deques:\n"; XPtest();
  162.   if (ptimes) cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
  163.   start_timer();
  164.   cout << "DL deques:\n"; DLtest();
  165.   if (ptimes) cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
  166.   cout << "\nEnd of test\n";
  167.   return 0;
  168. }
  169.